package com.example.demo.service.impl;

import com.example.demo.entity.PageInfo;
import com.example.demo.entity.RelSendRecords;
import com.example.demo.entity.SendRecordsInfo;
import com.example.demo.mapper.RelSendRecordsMapper;
import com.example.demo.mapper.SendRecordsMapper;
import com.example.demo.service.SendRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author
 * @Description
 */
@Service
@Slf4j
public class SendRecordsServiceImpl implements SendRecordsService {
    @Autowired
    private SendRecordsMapper sendRecordsMapper;

    @Autowired
    private RelSendRecordsMapper relSendRecordsMapper;

    // 循环依赖
//    @Autowired
//    private InvoiceService invoiceService;
    @Override
    @Transactional
    public Long addSendRecordsList(SendRecordsInfo sendRecordsInfo) {

        if (sendRecordsInfo.getInvoiceIdList().isEmpty()) {
            return 0L;
        }

        Long l = sendRecordsMapper.insertSendRecords(sendRecordsInfo);

        if (l > 0) {

            RelSendRecords relSendRecords;

            List<RelSendRecords> relSendRecordsList = new ArrayList<>();
            for (String s : sendRecordsInfo.getInvoiceIdList()) {
                relSendRecords = new RelSendRecords();
                relSendRecords.setInvoiceId(Long.valueOf(s));
                relSendRecords.setSendId(sendRecordsInfo.getSendId());
                relSendRecords.setFailedReason(sendRecordsInfo.getFailedReasonMap().get(s));
                relSendRecordsList.add(relSendRecords);
            }
            // invoiceInfoList-- invoiceCode invoiceNumber
            // invoiceIdList-- mysql --> invoiceVo
            relSendRecordsMapper.insertRelSendRecordsList(relSendRecordsList);
            return sendRecordsInfo.getSendId();
        } else {
            return l;
        }
    }

    @Override
    public List<SendRecordsInfo> findSendRecords(SendRecordsInfo sendRecordsInfo) {
        PageInfo pageInfo = sendRecordsInfo.getPageInfo();
        if (pageInfo != null) {
            if (pageInfo.getPageIndex() == null || pageInfo.getPageIndex() <= 0) {
                pageInfo.setPageIndex(1);
            }
            if (pageInfo.getPageSize() == null || pageInfo.getPageSize() <= 0) {
                pageInfo.setPageSize(10);
            }
            pageInfo.setPageOffset((pageInfo.getPageIndex() - 1) * pageInfo.getPageSize());
        }

        return sendRecordsMapper.findSendRecordsList(sendRecordsInfo, pageInfo);
    }

    @Override
    public List<String> findInvoiceIdListBySendId(Long sendId) {
        return relSendRecordsMapper.findInvoiceIdListBySendId(sendId);
    }

    @Override
    public List<RelSendRecords> findRelSendRecords(Long sendId) {
        List<RelSendRecords> relSendRecordsList = relSendRecordsMapper.findRelSendRecordsBySendId(sendId);
        List<String> invoiceIdList = relSendRecordsMapper.findInvoiceIdListBySendId(sendId);
        System.out.println(invoiceIdList);
        //
        // invoiceVo
//        List<InvoiceVo> invoiceVoList = invoiceMapper.findInvoiceVoListByInvoiceIds(invoiceIdList);
//        return invoiceVoList;
        return relSendRecordsList;
    }

    //    @Override
    @Transactional
    public Long resend(SendRecordsInfo sendRecordsInfo, String email) { // controller层
//        List<String> invoiceIdList = sendRecordsInfo.getInvoiceIdList();
        List<String> invoiceIdList = relSendRecordsMapper.findInvoiceIdListBySendId(sendRecordsInfo.getSendId());
        SendRecordsInfo _sendRecordsInfo = sendRecordsMapper.findSendRecordsById(sendRecordsInfo.getSendId());
        // 根据id获得invoiceInfo
        System.out.println("得到发票；invoiceIdList-->invoiceInfoList");
        if (!StringUtils.hasText(email)) {
            email = _sendRecordsInfo.getEmail();
        }
        System.out.println("交付接口");
        System.out.println(invoiceIdList);
        System.out.println(email);
        // sendId
        _sendRecordsInfo.setReSend(true);
//        _sendRecordsInfo.setReSendId(); // 重新交付的新id
        // 需修改交付接口 batchSendInvoiceNew(AuthUserInfo, AuthEnterpriseInfo, 实例)
        // emailString invoiceInfoList sendId source
        // 手动 自动 结算单 重新交付
        return sendRecordsMapper.updateSendRecords(_sendRecordsInfo);
    }

//    public List<InvoiceVo> findInvoiceDetailList(SendRecordsInfo sendRecordsInfo) {
//        String[] invoiceId = sendRecordsInfo.getInvoiceId().split(";");
//        //
//        List<InvoiceVo> invoiceVoList = invoiceMapper.findInvoiceVoListByInvoiceIds(List.of(invoiceId));
//        return invoiceVoList;
//    }
}
